package com.zhuchaooasystem.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Environment;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/************************************
 * 图片处理相关工具类
 *
 * @author 尹思
 ************************************/
public class BitmapUtils {

    private BitmapUtils() {
        throw new AssertionError();
    }

    /**
     * drawable转bitmap
     *
     * @param drawable
     * @return
     */
    public static Bitmap drawableToBitamp(Drawable drawable) {
        Bitmap bitmap = null;
        try {
            int w = drawable.getIntrinsicWidth();
            int h = drawable.getIntrinsicHeight();
            Bitmap.Config config =
                    drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                            : Bitmap.Config.RGB_565;
            bitmap = Bitmap.createBitmap(w, h, config);
            //注意，下面三行代码要用到，否在在View或者surfaceview里的canvas.drawBitmap会看不到图
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, w, h);
            drawable.draw(canvas);
        } catch (Exception e) {
        }
        return bitmap;
    }

    /**
     * 压缩图片
     *
     * @param activity
     * @param srcPath
     * @return
     */
    public static String compress(Activity activity, String srcPath,String dirPath) {
        String new_path = "";
        boolean isReturnNewPath = false;
        ByteArrayOutputStream baos = null;
        try {
            //当文件的大小超过300k进行压缩
            if (new File(srcPath).length() > 295 * 1024) {
                // 文件后缀名
                String extra = srcPath.substring(srcPath.lastIndexOf("."));
                if (extra.toUpperCase().equals(".GIF")) {
                    extra = ".png";
                }
                float hh = ScreenUtils.getScreenWidth(activity);
                float ww = ScreenUtils.getScreenHeight(activity);
                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inJustDecodeBounds = true;
                Bitmap bitmap = BitmapFactory.decodeFile(srcPath, opts);
                opts.inJustDecodeBounds = false;
                int w = opts.outWidth;
                int h = opts.outHeight;
                int size = 0;
                if (w <= ww && h <= hh) {
                    size = 1;
                } else {
                    double scale = w >= h ? w / ww : h / hh;
                    double log = Math.log(scale) / Math.log(2);
                    double logCeil = Math.ceil(log);
                    size = (int) Math.pow(2, logCeil);
                }
                opts.inSampleSize = size;
                bitmap = BitmapFactory.decodeFile(srcPath, opts);
                baos = new ByteArrayOutputStream();
                int quality = 100;
                bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
                while (baos.toByteArray().length > 295 * 1024) {
                    baos.reset();
                    quality -= 5;
                    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
                }
                // ----创建存放图片的文件夹
                new_path =dirPath;
                FileUtils.createFloderImages(new_path);
                // ----设置图片文件的名称
                new_path = new_path + "/" + System.currentTimeMillis() + extra;
                baos.writeTo(new FileOutputStream(new_path));
                isReturnNewPath = true;
            } else {
                isReturnNewPath = false;
            }
        } catch (Exception e) {
        } finally {
            try {
                if (null != baos) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
            }
        }
        if (isReturnNewPath) {
            return new_path;
        } else {
            return srcPath;
        }
    }

}
